package jude.java.money.net.http;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;

public class HttpCommunication {
	public static final String METHOD_GET = "get";
	public static final String METHOD_POST = "post";

	private String url;
	private String method;
	private boolean followRedirects;
	private String successPattern;
	private String failPattern;
	private String parameters;
	private String charset;
	private String prefer;
	
	private NameValuePair[] paramPairs = null;
	
	public boolean isGetMethod(){
		return METHOD_GET.equals(method);
	}
	
	@SuppressWarnings("unused")
	private boolean isPostMethod(){
		return !isGetMethod();
	}
	
	public boolean isFailPage(String response){
		if(StringUtils.isEmpty(failPattern))
			return false;
		Pattern p = Pattern.compile(failPattern);
		return p.matcher(response).find();
	}
	
	public boolean isSuccessPage(String response){
		if(StringUtils.isEmpty(failPattern))
			return true;
		Pattern p = Pattern.compile(successPattern);
		return p.matcher(response).find();
	}
	
	public HttpMethodBase getHttpMethod(){
		HttpMethodBase method = null;
		if ( HttpCommunication.METHOD_GET.equalsIgnoreCase(this.method) ) {
			method = new GetMethod(url);
			if(StringUtils.isNotEmpty(parameters)){
				method.setQueryString(getParameterPairs());
			}
		} else {
			method = new PostMethod(url);
			((PostMethod)method).setRequestBody(getParameterPairs());
		}
		setRequestHeader(method);
		if(isGetMethod())
			method.setFollowRedirects(followRedirects);
		return method;
	}
	
	public static GetMethod getRedirectMethod(String url){
		GetMethod method = new GetMethod(url);
		setRequestHeader(method);
		method.setFollowRedirects(true);
		return method;
	}

	private static void setRequestHeader(HttpMethodBase method) {
		method.addRequestHeader("accept", "*/*");
		method.addRequestHeader("accept-language", "zh-cn");
		method.addRequestHeader(
						"user-agent",
						"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)");
		method.addRequestHeader("connection", "Keep-Alive");
		method.addRequestHeader("Keep-Alive", "timeout=15, max=3000");
	}
	
	@SuppressWarnings("unused")
	private boolean isRedirectPage(String response) {
	    String pattern = "http\\-equiv=\"Refresh\"\\scontent";
	    Pattern p = Pattern.compile(pattern);
	    return p.matcher(response).find();
	}
	
	public NameValuePair[] getParameterPairs(){
		if(paramPairs == null){
			paramPairs = translate(parameters);
		}
		return paramPairs;
	}
	
	public static NameValuePair[] translate(String parameters){
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		if(StringUtils.isNotEmpty(parameters)){
			String[] ss = StringUtils.split(parameters, '&');
			for (String s : ss) {
				String[] sss = StringUtils.split(s, "=", 2);
				if(sss.length == 2)
					pairs.add(new NameValuePair(sss[0], sss[1].replaceAll("#26", "&")));
			}
		}
		return (NameValuePair[])pairs.toArray(new NameValuePair[pairs.size()]);
	}
	
	public String getCharset() {
		return charset;
	}
	public void setCharset(String charset) {
		this.charset = charset;
	}
	public String getFailPattern() {
		return failPattern;
	}
	public void setFailPattern(String failPattern) {
		this.failPattern = failPattern;
	}
	public boolean isFollowRedirects() {
		return followRedirects;
	}
	public void setFollowRedirects(boolean followRedirects) {
		this.followRedirects = followRedirects;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getParameters() {
		return parameters;
	}
	public void setParameters(String parameters) {
		this.parameters = parameters;
	}
	public String getSuccessPattern() {
		return successPattern;
	}
	public void setSuccessPattern(String successPattern) {
		this.successPattern = successPattern;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

	public String getPrefer() {
		return prefer;
	}

	public void setPrefer(String prefer) {
		this.prefer = prefer;
	}

}
